<?php

namespace App\Http\Controllers\Admin;

use App\Models\HotKeyword;
use Illuminate\Http\Request;

class HotKeywordController extends BaseController
{
    public function __construct()
    {
        parent::__construct();
        $this->middleware('check_admin_login');
    }

    public function index(Request $request)
    {
        $page                         = intval($request->get('page',1));
        $page                         = max($page ,1);
        $page_size                    = intval($request->get('limit',10));

        $builder = HotKeyword::query()->where('deleted',0);
        $columns = ['*'];
        $list = $builder->orderBy('id','desc')->paginate($page_size,$columns,'page',$page);

        $items = [];
        if(!empty($list->items()))
        {
            foreach ($list->items() as $item)
            {
                $items[] = [
                    'id'        => $item->id,
                    'keyword'   => $item->keyword,
                ];
            }
        }

        $data = [
            '_meta' => [
                'total_count'  => $list->total(),
                'page_count'   => $list->lastPage(),
                'current_page' => $list->currentPage(),
                'per_page'     => $list->perPage()
            ],
            "items" => $items
        ];

        return $this->res([
            'data' => $data
        ]);
    }

    public function store(Request $request)
    {
        $keyword = trim($request->get('keyword'));
        if(empty($keyword))
        {
            return $this->err(1,200,'','参数错误');
        }

        $data = [
            'keyword' => $keyword
        ];
        $ret = HotKeyword::create($data);
        if($ret)
        {
            $ret->update(['seq' => $ret->id]);
            return $this->ok([],'添加成功');
        }
        return $this->err(1,200,'','添加失败');
    }

    public function update(Request $request)
    {
        $keyword = trim($request->get('keyword'));
        $id = intval($request->get('id'));

        if(empty($keyword) || empty($id))
        {
            return $this->err(1,200,'','参数错误');
        }

        $row = HotKeyword::query()->where('id',$id)->where('deleted',0)->first();
        if(empty($row))
        {
            return $this->err(1,200,'','记录不存在');
        }

        $ret = $row->update(['keyword' => $keyword]);
        if($ret)
        {
            return $this->ok([],'修改成功');
        }
        return $this->err(1,200,'','修改失败');
    }

    public function show(Request $request)
    {
        $id = intval($request->get('id'));
        if(empty($id))
        {
            return $this->err(1,200,'','参数错误');
        }

        $row = HotKeyword::query()->where('id',$id)->where('deleted',0)->first();
        if(empty($row))
        {
            return $this->err(1,200,'','记录不存在');
        }

        $result = [
            'id'        => $row->id,
            'keyword'   => $row->keyword,
        ];
        return $this->ok($result);
    }

    public function destroy(Request $request)
    {
        $id = intval($request->get('id'));

        if(empty($id))
        {
            return $this->err(1,200,'','参数错误');
        }

        $row = HotKeyword::query()->where('id',$id)->where('deleted',0)->first();
        if(empty($row))
        {
            return $this->err(1,200,'','记录不存在');
        }

        $ret = $row->update(['deleted' => 1]);
        if($ret)
        {
            return $this->ok([],'删除成功');
        }
        return $this->err(1,200,'','删除失败');
    }
}
